using System;
using System.Threading;
using GameFrameworkLibraly.BaseObjects;
using GameFrameworkLibraly.Controls.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;

namespace GameFrameworkLibraly.GlobalGameObjects
{
    public abstract class LoadContentClassBase : GameObjectBase, IProcessGesture 
    {
        protected Thread LoadingThread;
        protected Color ColorFilterField = Color.White;
        protected Color TextColorField = Color.White;

        
        protected LoadContentClassBase(MyGame game)
            : base(game)
        {
        }

        /// <summary>
        /// Occures when everything is loaded
        /// </summary>
        public event EventHandler Loaded;

        protected virtual void InvokeLoaded(EventArgs e)
        {
            EventHandler handler = Loaded;
            if (handler != null) handler(this, e);
        }

        /// <summary>
        /// Occures when LoadContentClass is closed
        /// </summary>
        public event EventHandler Closed;

        protected virtual void InvokeClosed(EventArgs e)
        {
            EventHandler handler = Closed;
            if (handler != null) handler(this, e);
        }

        /// <summary>
        /// Progress of content loading (0..100)
        /// </summary>
        public int LoadingProgress { get; protected set; }

        /// <summary>
        /// Background texture (Company Logo)
        /// </summary>
        public Texture2D BackgroundTexture { get; set; }

        /// <summary>
        /// Background texture that appears when everything is loaded (Game Logo)
        /// </summary>
        public Texture2D BackgroundTexture2 { get; set; }

        /// <summary>
        /// Where to draw BackgroundTexture
        /// </summary>
        public Rectangle DestinationRectangle { get; set; }

        /// <summary>
        /// Text that will be written when the first screen will be shown
        /// </summary>
        public string Text { get; set; }

        /// <summary>
        /// Text position
        /// </summary>
        public Vector2 TextPosition { get; set; }

        /// <summary>
        /// Font that will be used to draw the text
        /// </summary>
        public SpriteFont SpriteFont { get; set; }

        /// <summary>
        /// Color of the text
        /// </summary>
        public Color FontColor
        {
            get { return TextColorField; } 
            set { TextColorField = value; }
        }

        /// <summary>
        /// Color filter for BackgroundTexture
        /// </summary>
        public Color ColorFilter
        {
            get { return ColorFilterField; }
            set { ColorFilterField = value; }
        }

        /// <summary>
        /// Load content in Background thread
        /// </summary>
        public void LoadInBGThread()
        {
            LoadingThread = new Thread(Load);
            LoadingThread.Start();
        }

        /// <summary>
        /// Load Content
        /// </summary>
        public abstract void Load();

        /// <summary>
        /// Draw BackGround
        /// </summary>
        /// <param name="spriteBatch"></param>
        public virtual void Draw(SpriteBatch spriteBatch)
        {
            Texture2D textureToDraw = LoadingProgress < 100 ? BackgroundTexture : BackgroundTexture2;

            if (textureToDraw != null && !DestinationRectangle.IsEmpty)
                spriteBatch.Draw(textureToDraw, DestinationRectangle, ColorFilter);

            if (SpriteFont != null && !string.IsNullOrEmpty(Text))
            {
                spriteBatch.DrawString(SpriteFont, Text, TextPosition, FontColor);
            }
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            if (SpriteFont != null && !string.IsNullOrEmpty(Text))
            {
                Vector2 textSize = SpriteFont.MeasureString(Text);
                TextPosition = new Vector2((int)(DestinationRectangle.Left + (DestinationRectangle.Width - textSize.X) / 2),
                                            TextPosition.Y);
            }
        }

        #region Implementation of IProcessGesture

        public virtual bool ProcessGesture(GestureSample gestureSample)
        {
            if (gestureSample.GestureType == GestureType.Tap && LoadingProgress == 100)
            {
                InvokeClosed(EventArgs.Empty);
                return true;
            }
            return false;
        }

        #endregion
    }
}
